// pages/index/index.js
var utils = require('../../utils/util');
var initApp = require('../../utils/initlogin');

Page({
  gotoPage: utils.gotoPage,
  initAppUser: initApp.initAppUser,

  /**
   * 页面的初始数据
   */
  data: {
    timecount: {
      days: 0,
      hours: 0,
      minutes: 0,
      seconds: 0
    },
    tuanend: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // 隐藏底部tabbar
    wx.hideTabBar({});

    var that = this;
    var HOST = getApp().globalData.HOST;
    console.log('HOST:', HOST)

    var pid = options.pid;
    var gid = options.gid || '';
    this.setData({
      pid: pid,
      gid: gid
    });

    // 初始化用户信息
    that.initAppUser();
    // 获取商品信息
    that.getProductInfo(pid, gid);
  },

  //获取商品信息
  getProductInfo: function(pid, gid) {
    var that = this;
    var HOST = getApp().globalData.HOST;
    var app = getApp();

    wx.request({
      url: HOST + 'product',
      data: {
        ACTION: 'ONE',
        id: pid,
        groupbuyid: gid
      },
      success(resp) {
        console.log('home pid:', resp);
        var product = resp.data.data.product;
        var groupbuy = resp.data.data.groupbuy;
        product.covers = product.covers.split(', ');
        product.detail = product.detail.replace(/<img/ig, '<img style="width:100%;"');
        console.log('home product:', product, groupbuy);
        that.setData({
          product: product,
          groupbuy: groupbuy
        });
        // 给订单用
        wx.setStorage({
          key: 'product',
          data: product
        });

        // 结束倒计时设置
        if (app.timecountInt) {
          clearInterval(app.timecountInt);
        }
        app.timecountInt = setInterval(function() {
          var endtime = new Date(groupbuy.enddate);
          var minseconds = endtime.getTime() - (new Date()).getTime();
          if (minseconds > 0) {
            console.log('minseconds + :', minseconds)
            that.setData({
              timecount: {
                days: parseInt(minseconds / (24 * 60 * 60 * 1000)),
                hours: parseInt(minseconds % (24 * 60 * 60 * 1000) / (60 * 60 * 1000)),
                minutes: parseInt(minseconds % (24 * 60 * 60 * 1000) / (60 * 60 * 1000)),
                seconds: parseInt(minseconds % (24 * 60 * 60 * 1000) % (60 * 60 * 1000) / (60 * 1000))
              }
            });
          } else {
            console.log('minseconds - :', minseconds, app.timecountInt)
            clearInterval(app.timecountInt);
            that.setData({
              tuanend: 1
            });
          }
        }, 1000);
      },
    });
  },

  // 点击购买
  buyNow: function(event) {
    var that = this;
    var pid = that.data.product.id;
    var gid = that.data.groupbuy.id;
    console.log('pid,gid:', pid, gid)
    wx.navigateTo({
      url: '/pages/buy/buy?pid=' + pid + '&gid=' + gid,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    var app = getApp();

    // 结束倒计时
    if (app.timecountInt) {
      clearInterval(app.timecountInt);
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: this.data.product.title + ' ' + this.data.product.subtitle,
      path: '/pages/home/home?pid=' + this.data.pid + '&gid=' + this.data.gid,
      imageUrl: this.data.product.covers[0],
    }
  },
})